﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TSV.Areas.Administration.Models;
using TSV.Library.Common;
using TSV.Models;
using System.IO;
using System.Collections;
using TSV.Library.Attributes;


namespace TSV.Areas.Administration.Controllers
{
    public class EmailRegisterController : BaseController
    {
        EmailRegisterDao ER = new EmailRegisterDao();
        public ActionResult GetListJQGrid()
        {

            #region JQGrid Params

            string sortColumn = (Request.Params[GridConstants.SORT_COLUMN]).ToString();
            string sortOrder = (Request.Params[GridConstants.SORT_ORDER]).ToString();
            int pageIndex = Convert.ToInt32(Request.Params[GridConstants.PAGE_INDEX]);
            int rowCount = Convert.ToInt32(Request.Params[GridConstants.ROW_COUNT]);
            #endregion

            List<sp_GetEmailResult> ERList = ER.GetList().ToList<sp_GetEmailResult>();

            int totalRecords = ERList.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rowCount);

            var finalList = ER.Sort(ERList, sortColumn, sortOrder)
                                  .Skip((pageIndex - 1) * rowCount)
                                   .Take(rowCount);
            int j = 0;

            var jsonData = new
            {
                total = totalPages,
                page = pageIndex,
                records = totalRecords,
                rows = (
                    from m in finalList
                    select new
                    {

                        i = m.ID,
                        cell = new string[] {
                                Convert.ToString(j+=1),
                                m.ID.ToString(), 
                                TooltipLink(m.ID.ToString(),"/Administration/EmailRegister/Edit",HttpUtility.HtmlEncode(m.Email)),
                                ViewButtonEdit("/Administration/EmailRegister/Edit/"+m.ID.ToString()),
                                
                            }
                    }
                ).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        private string TooltipLink(string id, string actionUrl, string textLink)
        {
            string onclick = string.Format("JS_LIB.popup('{0}/{1}','{2}',{3});", actionUrl, id, "Cập nhật email", 500);
            string jsvoid = "javascript:void(0);";
            return string.Format("<a id=\"{0}\" href=\"{1}\" class=\"showTooltip\" onclick=\"{2}\">{3}</a>", id, jsvoid, onclick, textLink);

        }
        private string ViewButtonEdit(string actionUrl)
        {
            string onclick = string.Format("JS_LIB.popup('{0}','{1}',{2});", actionUrl, "Cập nhật email", 500);

            return CommonFunc.ButtonEditPopup(actionUrl, "Cập nhật", 500);

        }
        [CrmAuthorizeAttribute(Module = Modules.QL_Email_Dang_Ky, Rights = Permissions.Xoa, ShowAtCurrentPage = true)]
        public ActionResult DeleteList(string id)
        {
            Message msg = ER.DeleteList(id);
            ShowMessage(msg);
            return RedirectToAction("Index");
        }
        [CrmAuthorizeAttribute(Module = Modules.QL_Email_Dang_Ky, Rights = Permissions.Doc)]
        public ActionResult Index()
        {
            return View();
        }
        [CrmAuthorizeAttribute(Module = Modules.QL_Email_Dang_Ky, Rights = Permissions.Doc, ShowAtCurrentPage = true)]
        public ActionResult Edit(int id)
        {
            Email oscObj = ER.GetById(id.ToString());
            return View(oscObj);
        }
        [CrmAuthorizeAttribute(Module = Modules.QL_Email_Dang_Ky, Rights = Permissions.Sua, ShowAtCurrentPage = true)]
        [HttpPost]
        public ActionResult Edit(Email oscObj, FormCollection collection)
        {
            try
            {

                // TODO: Add update logic here
                Message msg = ER.Update(oscObj);
                ShowMessage(msg);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        [CrmAuthorizeAttribute(Module = Modules.QL_Email_Dang_Ky, Rights = Permissions.Them, ShowInPopup = true)]
        public ActionResult Create()
        {
            return View();
        }
        [CrmAuthorizeAttribute(Module = Modules.QL_Email_Dang_Ky, Rights = Permissions.Them, ShowAtCurrentPage = true)]
        [HttpPost]
        public ActionResult Create(Email em, FormCollection collection)
        {
            EmailRegisterDao dao = new EmailRegisterDao();
            Message msg = null;
            msg = dao.Insert(em);
            ShowMessage(msg);
            return RedirectToAction("Index"); ;

        }

    }
}
